﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AjaxControlToolkit;
using System.Web.SessionState;
using System.Net.Mail;
using System.IO;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI;
using PuntoMascotasWeb.WebSite.Includes.Clases;
using System.Web.Security;


namespace PuntoMascotasWeb.WebSite.Includes.Funciones
{
  public class Funciones: System.Web.UI.Page
  {
    public static Usuario.Usuarios ValidarUsuario(MasterPage poMaster, HttpSessionState poSession) 
    {
      Label oLabel = (Label)poMaster.FindControl("mplblUsuario");

      if (poSession["Usuario"] != null)
      {
        if (poSession["UsuarioDescripcion"] != null)
        {
          oLabel.Text = poSession["UsuarioDescripcion"].ToString();
        }
        else
        {
          oLabel.Text = String.Empty;
        }
        return (Usuario.Usuarios)poSession["Usuario"];
      }
      else 
      {
        FormsAuthentication.RedirectToLoginPage();
        return Usuario.Usuarios.Nulo;
      }      
    }

    public  void ProcesarExcepciones(Exception ex) 
    {
      ShowMessage(ex.Message);
    }

    public void EjecutarScript(string psz_Script)
    {
      StringBuilder sb = new StringBuilder();
      sb.Append(psz_Script);
      AjaxControlToolkit.ToolkitScriptManager.RegisterClientScriptBlock(this, this.GetType(), System.Guid.NewGuid().ToString(), sb.ToString(), true);
    }

    public  void EjecutarScriptFinal(string psz_Script)
    {
      StringBuilder sb = new StringBuilder();
      sb.Append(psz_Script);
      AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), sb.ToString(), true);
    }

    public  void ShowMessage(string psz_Message)
    {
      string sz_Script = string.Format(@"alert(""{0}"");", QuoteJScriptString(psz_Message, false));

      AjaxControlToolkit.ToolkitScriptManager.RegisterClientScriptBlock(this, this.GetType(), System.Guid.NewGuid().ToString(), sz_Script, true);

    }

    public static string QuoteJScriptString(string value, bool forUrl)
    {
      StringBuilder builder = null;
      if (string.IsNullOrEmpty(value))
      {
        return string.Empty;
      }
      int startIndex = 0;
      int count = 0;
      for (int i = 0; i < value.Length; i++)
      {
        switch (value[i])
        {
          case '%':
            {
              if (!forUrl)
              {
                break;
              }
              if (builder == null)
              {
                builder = new StringBuilder(value.Length + 6);
              }
              if (count > 0)
              {
                builder.Append(value, startIndex, count);
              }
              builder.Append("%25");
              startIndex = i + 1;
              count = 0;
              continue;
            }
          case '\'':
            {
              if (builder == null)
              {
                builder = new StringBuilder(value.Length + 5);
              }
              if (count > 0)
              {
                builder.Append(value, startIndex, count);
              }
              builder.Append(@"\'");
              startIndex = i + 1;
              count = 0;
              continue;
            }
          case '\\':
            {
              if (builder == null)
              {
                builder = new StringBuilder(value.Length + 5);
              }
              if (count > 0)
              {
                builder.Append(value, startIndex, count);
              }
              builder.Append(@"\\");
              startIndex = i + 1;
              count = 0;
              continue;
            }
          case '\t':
            {
              if (builder == null)
              {
                builder = new StringBuilder(value.Length + 5);
              }
              if (count > 0)
              {
                builder.Append(value, startIndex, count);
              }
              builder.Append(@"\t");
              startIndex = i + 1;
              count = 0;
              continue;
            }
          case '\n':
            {
              if (builder == null)
              {
                builder = new StringBuilder(value.Length + 5);
              }
              if (count > 0)
              {
                builder.Append(value, startIndex, count);
              }
              builder.Append(@"\n");
              startIndex = i + 1;
              count = 0;
              continue;
            }
          case '\r':
            {
              if (builder == null)
              {
                builder = new StringBuilder(value.Length + 5);
              }
              if (count > 0)
              {
                builder.Append(value, startIndex, count);
              }
              builder.Append(@"\r");
              startIndex = i + 1;
              count = 0;
              continue;
            }
          case '"':
            {
              if (builder == null)
              {
                builder = new StringBuilder(value.Length + 5);
              }
              if (count > 0)
              {
                builder.Append(value, startIndex, count);
              }
              builder.Append("\\\"");
              startIndex = i + 1;
              count = 0;
              continue;
            }
        }
        count++;
      }
      if (builder == null)
      {
        return value;
      }
      if (count > 0)
      {
        builder.Append(value, startIndex, count);
      }
      return builder.ToString();
    }
  }
}